<?php
$GLOBALS['httpxmlClasses'][] = 'Erra_Core_XMLResponse';

/**
 * Erra_Core_XMLResponse - Returns XML response for AJAX forms
 *
 * @package
 * @author Alexis Eric
 * @copyright Copyright (c) 2012 - Erra Prod
 * @version $1.0$
 * @access public
 */
class Erra_Core_XMLResponse {
    private $x;
    private $save;

    public function __construct()
    {
        $this->x = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><rsp></rsp>');
        $this->save = false;
    }

    public function status($status)
    {
        $this->x->addChild('status', $status);
    }

    public function addFile($status)
    {
        header('Content-type: text/html');
        exit('<html><head></head><body><div id="status">' . $status . '</div></body></html>');
    }

    public function addError($field, $error = '')
    {
        if (isset($this->x->error)) {
            $this->x->error->addChild($field, $error);
        } else {
            $e = $this->x->addChild('error');
            $e->addChild($field, $error);
        }
    }

    public function addContent($div, $content)
    {
        $content = htmlspecialchars($content);
        if (isset($this->x->content)) {
            $c = $this->x->content->addChild('c', $content);
        } else {
            $e = $this->x->addChild('content');
            $c = $e->addChild('c', $content);
        }
        $c->addAttribute('selector', $div);
    }

    public function addReplace($div, $content)
    {
        $content = htmlspecialchars($content);
        if (isset($this->x->replace)) {
            $c = $this->x->replace->addChild('c', $content);
        } else {
            $e = $this->x->addChild('replace');
            $c = $e->addChild("c", $content);
        }
        $c->addAttribute('selector', $div);
    }

    public function addConfirmation($message)
    {
        $this->x->addChild('confirmation', $message);
    }

    public function addOk($field)
    {
        if (isset($this->x->ok)) {
            $this->x->ok->addChild($field);
        } else {
            $e = $this->x->addChild('ok');
            $e->addChild($field);
        }
    }

    public function addShow($div)
    {
        if (isset($this->x->show)) {
            $this->x->show->addChild($div);
        } else {
            $e = $this->x->addChild('show');
            $e->addChild($div);
        }
    }

    public function addReload()
    {
        $this->addRedirection($_SERVER['HTTP_REFERER']);
    }

    public function addRedirection($url = null)
    {
        if (is_null($url)) {
            $this->addReload();
        } else {
            $this->x->addChild('redirection', $url);
        }
    }

    public function addAppend($div, $content)
    {
        $content = htmlspecialchars($content);
        if (isset($this->x->append)) {
            $this->x->append->addChild($div, $content);
        } else {
            $e = $this->x->addChild('append');
            $e->addChild($div, $content);
        }
    }

    public function addParam($id, $param, $value)
    {
        $value = htmlspecialchars($value);
        if (isset($this->x->param)) {
            if (isset($this->x->param->$id)) {
                $this->x->param->$id->addChild($param, $value);
            } else {
                $e = $this->x->param->addChild($id);
                $e->addChild($param, $value);
            }
        } else {
            $e = $this->x->addChild('param');
            $f = $e->addChild($id);
            $f->addChild($param, $value);
        }
    }

    public function addAlert($texte)
    {
        $texte = htmlspecialchars($texte);
        if (isset($this->x->alert)) {
            $this->x->alert->addChild('content', $texte);
        } else {
            $e = $this->x->addChild('alert');
            $e->addChild('content', $texte);
        }
    }

    public function addVariable($varname, $value)
    {
        $texte = htmlspecialchars($value);
        if (isset($this->x->variable)) {
            $this->x->variable->addChild($varname, $texte);
        } else {
            $e = $this->x->addChild('variable');
            $e->addChild($varname, $texte);
        }
    }

    public function addDebug($texte)
    {
        $texte = htmlspecialchars($texte);
        $this->x->addChild('debug', $texte);
    }

    public function addEval($code)
    {
        if (isset($this->x->eval)) {
            $this->x->eval->addChild('code', $code);
        } else {
            $e = $this->x->addChild('eval');
            $e->addChild('code', $code);
        }
    }

    public function addClosePopup($code=null)
    {
        if (isset($this->x->closepopup)) {
            $this->x->closepopup->addChild('close');
        } else {
            $e = $this->x->addChild('closepopup');
            $e->addChild('code', $code);
        }
    }

    public function addOpenPopup($content)
    {
        if (isset($this->x->openpopup)) {
            $this->x->openpopup->addChild('content', htmlspecialchars($content));
        } else {
            $e = $this->x->addChild('openpopup');
            $e->addChild('content', htmlspecialchars($content));
        }
    }

    public function addChangePopup($content)
    {
        if (isset($this->x->changepopup)) {
            $this->x->changepopup->addChild('content', htmlspecialchars($content));
        } else {
            $e = $this->x->addChild('changepopup');
            $e->addChild('content', htmlspecialchars($content));
        }
    }

    public function addPopupDimensions()
    {
        if (isset($this->x->popupdimensions)) {
            return;
        }
        $this->x->addChild('popupdimensions', 'a');
    }

    public function addTruePopup($url)
    {
        if (isset($this->x->truepopup)) {
            $this->x->truepopup->addChild('url', $url);
        } else {
            $e = $this->x->addChild('truepopup');
            $e->addChild('truepopup', $url);
        }
    }

    public function addTimer($time)
    {
        $this->x->addChild('timer', intval($time * 1000));
    }

    public function addValue($id, $value)
    {
        if (isset($this->x->value)) {
            $this->x->value->addChild($id, $value);
        } else {
            $e = $this->x->addChild('value');
            $e->addChild($id, $value);
        }
    }

    public function save()
    {
        $this->save = true;
    }

    public function asXML()
    {
        global $ajaxErrors;
        if (isset($ajaxErrors) && count($ajaxErrors)) {
            $e = $this->x->addChild('phpError');
            foreach($ajaxErrors as $er) {
                $e->addChild('e', htmlspecialchars($er));
            }
        }

        if ($this->save) {
            $_SESSION['lastAjax'] = $this->x->asXML();
        }
        return $this->x->asXML();
    }

    public static function getArgs($args)
    {
        $res = array();
        $dec = explode('/', trim($args, '/'));
        foreach($dec as $d) {
            if ($d == 'null') {
                $res[] = 'null';
            } else {
                $res[] = $d;
            }
        }
        return $res;
    }

    public static function lastAjax($args)
    {
        if (isset($_SESSION['lastAjax'])) {
            echo $_SESSION['lastAjax'];
        } else {
            echo '<?xml version="1.0" encoding="UTF-8" ?><rsp></rsp>';
        }
        exit;
    }

    public static function ajax($args)
    {
        header('Content-type: text/xml');
        $args = explode('/', trim($args, '/'));

        $x = new Erra_Core_XMLResponse();
        $f = $args[0];
        $ok = false;

        foreach($GLOBALS['httpxmlClasses'] as $c) {
            if (is_callable(array($c, $f))) {
                $x->status('1-1');
                call_user_func(array($c, $f), $args, $x);
                $ok = true;
                break;
            }
        }
        if (!$ok) {
            $x->status('1-0');
        }

        ob_clean();
        echo trim($x->asXML());
        exit;
    }
}

?>